package logs

import (
	"common/config"
	"github.com/charmbracelet/log"
	"os"
	"time"
)

const (
	DEBUG = "DEBUG"
	INFO  = "INFO"
	WARN  = "WARN"
	ERROR = "ERROR"
)

var logger *log.Logger

// InitLog 初始化日志，需要加载配置文件后运行
func InitLog() {
	logger = log.New(os.Stderr)
	level := config.Conf.Log.Level
	appName := config.Conf.AppName
	// 1. 设置日志级别
	switch level {
	case DEBUG:
		logger.SetLevel(log.DebugLevel)
	case INFO:
		logger.SetLevel(log.InfoLevel)
	case WARN:
		logger.SetLevel(log.WarnLevel)
	case ERROR:
		logger.SetLevel(log.ErrorLevel)
	default:
		logger.SetLevel(log.InfoLevel)
	}
	// 2. 设置前缀
	logger.SetPrefix(appName)
	// 3. 展示时间戳
	logger.SetReportTimestamp(true)
	// 4. 设置时间格式
	logger.SetTimeFormat(time.DateTime)
}

func Debug(format string, values ...any) {
	logMessage(logger.Debugf, format, values...)
}

func Info(format string, values ...any) {
	logMessage(logger.Infof, format, values...)
}

func Warn(format string, values ...any) {
	logMessage(logger.Warnf, format, values...)
}

func Error(format string, values ...any) {
	logMessage(logger.Errorf, format, values...)
}

func Fatal(format string, values ...any) {
	logMessage(logger.Fatalf, format, values...)
}

// 日志通用方法
func logMessage(logFunc func(format string, v ...any), format string, values ...any) {
	logFunc(format, values...)
}
